<?php
class Remain extends MY_Controller
{
	public function __construct()
	{
		log_message('debug', '=====开始调用Report->Remain->__construct接口=====');
		parent::__construct();
		$this->loadLanguageFile('application/controllers/Report/');
		log_message('debug', '=====调用Report->Remain->__construct接口结束=====');
	}
	
	public function showView() {
		$this->load->view('module/Report/remain.htm');
	}
	
	public function getListData() {
		log_message('debug', '=====开始调用Report->Remain->getListData接口=====');
		$this->verifyPrivilege();
		
		try {
			$gameAreaId = $this->param->getParam('gameAreaId', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 201, 'invalidErrorCode' => 1));
			$page = $this->param->getParam('page', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 1, 'invalidErrorCode' => 1), 'post', true);
			$rows = $this->param->getParam('rows', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 1, 'invalidErrorCode' => 1), 'post', true);
			$this->initGameAreaInfo($gameAreaId);
			
			$sql = "
				select
					<selectColumns>
				from
					`t_data_daily_report` a
				left join
					`t_data_game_area` b
				on
					a.`gameAreaId` = b.`gameAreaId`
				left join
					`t_data_operator` c
				on
					b.`operatorId` = c.`operatorId`
				where
					a.`gameAreaId` = '{$gameAreaId}'
				order by
					`datetime`
			";
			$getTotalSql = str_replace('<selectColumns>', "count(*)", $sql);
			$this->returnData['data']['total'] = $this->gmDatabase->getColumn($getTotalSql);
			
			$getDataSql = str_replace('<selectColumns>', "substr(a.`datetime`, 1, 10) as registerDate, a.`createRoleNum`, c.`operatorName`, b.`areaNum`", $sql);
			
			if($page && $rows) {
				$startRow = ($page - 1) * $rows;
				$getDataSql .= " limit {$startRow}, {$rows}";
			}
			
			$this->returnData['data']['rows'] = $this->gmDatabase->getArray($getDataSql);
			
			$sql = "
				select
					`registerDate`,
					`daysFromRegister`,
					`loginAccountNum`
				from
					`t_data_account_remain`
				where
                    `daysFromRegister` in (1, 2, 3, 4, 5, 6, 14, 29)
					and
					`gameAreaId` = '{$gameAreaId}'
			";
			$tmpArray = $this->gmDatabase->getArray($sql);
			$infoArray = array();
			
			foreach ($tmpArray as $v) {
				$infoArray[$v['registerDate']][$v['daysFromRegister']] = $v['loginAccountNum'];
			}
					
			foreach ($this->returnData['data']['rows'] as $k => $v) {
				for($i = 1; $i <= 29; $i++) {
					if(isset($infoArray[$this->returnData['data']['rows'][$k]['registerDate']][$i])) {
						$this->returnData['data']['rows'][$k]["day{$i}"] = number_format($infoArray[$this->returnData['data']['rows'][$k]['registerDate']][$i] / $this->returnData['data']['rows'][$k]['createRoleNum'] * 100, 2) . '%';
					}
				}
			}
		}
		
		catch(MY_Param_Exception $e) {
			$this->setErrorCode($e->getCode());
		}
		
		catch(MY_Mysql_Exception $e) {
			$this->setErrorCode(1);
		}
		
		catch(Exception $e) {
			$this->setErrorCode($e->getCode());
		}
		
		$this->returnJson();
		log_message('debug', '=====调用Report->Remain->getListData接口结束=====');
	}
}